package com.ucar.train.web;

import com.ucar.train.dto.Group;
import com.ucar.train.dto.UserGroup;
import com.ucar.train.dto.UserRole;
import com.ucar.train.service.GroupService;
import com.ucar.train.utils.GsonUtils;
import com.ucar.train.utils.JodaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author wufuqiang
 * @title: GroupController
 * @projectName:train-parent
 * @description: T0D0
 * @created 2019-07-25 14:06
 */
@Controller
@RequestMapping("/group")
public class GroupController {

    @Autowired
    GroupService groupService;

    GsonUtils gsonUtils = new GsonUtils();

    /**
     * 组管理页面
     * @return
     */
    @RequestMapping(value = "/listGroups", method = RequestMethod.GET)
    public String listGroups(Model model) {
        List<Group> groups = groupService.listGroups();
        model.addAttribute("groups",groups);
        return "group-list";

    }

    /**
     * 返回添加组页面
     * @return
     */
    @RequestMapping(value = "/saveGroup", method = RequestMethod.GET)
    public String saveGroup() {
        return "group-add";
    }

    /**
     * 添加组
     * @param group
     * @return
     */
    @RequestMapping(value = "/saveGroup", method = RequestMethod.POST)
    public String saveGroup(Group group) {
        group.setCreateTime(JodaUtils.nowAsDate());
        group.setModifyTime(JodaUtils.nowAsDate());
        groupService.saveGroup(group);
        return "redirect:listGroups";
    }

    /**
     * 返回修改组信息页面
     * @param id
     * @return
     */
    @RequestMapping(value = "/updateGroup", method = RequestMethod.GET)
    public String updateGroup(Model model,Long id) {
        Group group = groupService.getGroup(id);
        model.addAttribute("group",group);
        return "group-show";
    }

    /**
     * 修改组信息
     * @param group
     * @return 返回组管理页面
     */
    @RequestMapping(value = "/updateGroup", method = RequestMethod.POST)
    public String updateGroup(Group group) {
        group.setModifyTime(JodaUtils.nowAsDate());
        groupService.updateGroup(group);
        return "redirect:listGroups";
    }

    /**
     * 删除组,并删除与该组有关的用户组关系
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteGroup", method = RequestMethod.GET)
    public String deleteGroup(Long id) {
        groupService.deleteGroup(id);
        return "redirect:listGroups";
    }

    /**
     * 返回用户组管理页面
     * @return
     */
    @RequestMapping(value = "/listUserGroups", method = RequestMethod.GET)
    public String listUserGroups(Model model) {
        List<UserGroup> userGroups = groupService.listUserGroups();
        model.addAttribute("userGroups",userGroups);
        return "usergroup-list";
    }

    /**
     * 返回添加用户组信息页面
     * @return
     */
    @RequestMapping(value = "/saveUserGroup", method = RequestMethod.GET)
    public String saveUserGroup() {

        return "";
    }

    /**
     * 保存用户组关系
     * @param userGroup
     * @return
     */
    @RequestMapping(value = "/saveUserGroup", method = RequestMethod.POST)
    public String saveUserGroup(UserGroup userGroup) {
        userGroup.setCreateTime(JodaUtils.nowAsDate());
        userGroup.setModifyTime(JodaUtils.nowAsDate());
        groupService.saveUserGroup(userGroup);
        return "redirect:listUserGroups";
    }

    /**
     * 删除一条用户组关系
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteUserGroup", method = RequestMethod.GET)
    public String deleteUserGroup(Long id) {
        groupService.deleteUserGroup(id);
        return "redirect:listUserGroups";
    }

    /**
     * 返回所有用户角色信息
     * @param model
     * @return
     */
    @RequestMapping(value = "/listUserRoles", method = RequestMethod.GET)
    public String listUserRoles(Model model) {
        List<UserRole> userRoles = groupService.listUserRoles();
        model.addAttribute("userRoles",userRoles);
        return "userrole-list";
    }

    /**
     * 保存用户角色信息
     * @param userRole
     * @return
     */
    @RequestMapping(value = "/saveUserRole", method = RequestMethod.POST)
    public String saveUserRole(UserRole userRole) {
        userRole.setCreateTime(JodaUtils.nowAsDate());
        userRole.setModifyTime(JodaUtils.nowAsDate());
        groupService.saveUserRole(userRole);
        return "redirect:listUserRoles";
    }

    /**
     * 删除用户角色信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteUserRole", method = RequestMethod.GET)
    public String deleteUserRole(Long id) {
        groupService.deleteUserRole(id);
        return "redirect:listUserRoles";
    }

    /**
     * 返回修改用户角色信息页面
     * @param id
     * @return
     */
    @RequestMapping(value = "/updateUserRole", method = RequestMethod.GET)
    public String updateUserRole(Model model,long id) {
        UserRole userRole = groupService.getUserRole(id);//获取所要修改的用户角色信息
        model.addAttribute("userRole",userRole);
        return "userrole-show";
    }

    /**
     * 修改用户角色操作
     * @param userRole
     * @return
     */
    @RequestMapping(value = "/updateUserRole", method = RequestMethod.POST)
    public String updateUserRole(UserRole userRole) {
        userRole.setModifyTime(JodaUtils.nowAsDate());
        groupService.updateUserRole(userRole);
        return "redirect:listUserRoles";
    }

    /**
     * 组内用户角色查询
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/listUserRoleByGroup", method = RequestMethod.GET)
    public String listUserRolesByGroup(Model model,Long id) {
        List<UserRole> userRoles = groupService.listUserRolesByGroup(id);
        model.addAttribute("userRoles",userRoles);
        return "userrole-list";
    }

}
